﻿using ExcelSystem.Model;
using ExcelSystem.ToolHelper;
using ExcelSystem.ViewModelsClass;
using ImTools;
using Newtonsoft.Json;
using NPOI.Util;
using Prism.Commands;
using Prism.Common;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ExcelSystem.ViewModels
{
    public class BOMAddListViewModel : BindableBase, IDialogAware
    {
        public string Title =>"";

        ExcelContext db;

        private List<BOM> boms;
        public List<BOM> Boms
        {
            get { return boms; }
            set { SetProperty(ref boms, value); }
        }


        private List<NSMaterialsViewModel> nsMaterials;
        public List<NSMaterialsViewModel> NSMaterials
        {
            get { return nsMaterials; }
            set { SetProperty(ref nsMaterials, value); }
        }


        public event Action<IDialogResult> RequestClose;

        private DelegateCommand<string> saveCommand;
        /// <summary>
        ///  保存
        /// </summary>
        public DelegateCommand<string> SaveCommand
        {
            get { return saveCommand; }
            set { SetProperty(ref saveCommand, value); }
        }



        private readonly IRegionManager regionManage;
        private readonly IDialogService dialogService;
        BOMAddListViewModel(IRegionManager regionManage, IDialogService dialogService)
        {
            //数据库文档
            db = ExcelContentFactory.GetInstance() ;
            //依赖注入
            this.regionManage = regionManage;
            this.dialogService = dialogService;
            Boms=new List<BOM>();
            SaveCommand = new DelegateCommand<string>(Save);
            Init();


        }
        private void Init()
        {
         
            NSMaterials = new List<NSMaterialsViewModel>();
            db.NSMaterials.ToList().ForEach(t => {

                NSMaterials.Add(new NSMaterialsViewModel()
                {
                    NSMaterial = t,
                    AllName = t.Name + t.Type,
                });
            });

        }



        private void Save(string obj)
        {
            Boms.ForEach(t =>
            {
                // 补全信息
                var ns = NSMaterials.FirstOrDefault(n => n.NSMaterial.ID == t.ChannelSteelID);
                if (ns != null)
                {
                    t.ChannelSteel = ns.AllName;
                }

            });

           
            db.BOMs.AddRange(Boms);
            db.SaveChanges();
            MessageBox.Show("保存成功");

        }



        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
           
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            if (parameters != null && parameters.ContainsKey("id"))
            {

                var id = parameters.GetValue<List<string>>("id");
                if(id!=null)
                {
                    List<BOM> ids = new List<BOM>();
                    id.ForEach(t =>
                    {
                        Boms.Add(new BOM() { MaterialNo = t });

                    });
                  


                }

               
              
            }
        
        }

        private BOM SetProperty(BOM oldValue)
        {
            BOM newValue =new BOM();

            newValue.GetType().GetProperties().ForEach(t =>
            {
                oldValue.GetType().GetProperties().ForEach(p =>
                {
                    if (p.Name == t.Name&&p.Name!="ID")
                    {
                        var name = p.GetValue(oldValue);
                        t.SetValue(newValue, name);
                    }
                       

                });

            });


            return newValue;
        }

       
    }
}
